import request from "./axios";

export function getHttp(url: string, params = {}, headers = {}) {
    return request({
        url,
        method: "get",
        params: params,
        headers: headers,
    });
}

export function postHttp(url: string, data = {}, headers = {}) {
    console.log("请求地址:", url, data);
    const sId = localStorage.getItem('userInfo');
    console.log(sId);
    const userInfo = JSON.parse(sId || '{}');
    console.log(userInfo);
    return request({
        url,
        method: "post",
        data: data,
        headers: {
            ...headers,
            sessionId: userInfo.sessionId,
            userId: userInfo.userId,
            "Content-Type": "application/x-www-form-urlencoded",
        },
    });
}

export function putHttp(url: string, data = {}, headers = {}) {
    console.log("PUT请求地址:", url, data);
    const sId = localStorage.getItem('userInfo');
    const userInfo = JSON.parse(sId || '{}');
    return request({
        url,
        method: "put",
        data: data,
        headers: {
            ...headers,
            sessionId: userInfo.sessionId,
            userId: userInfo.userId,
            "Content-Type": "application/x-www-form-urlencoded",
        },
    });
}

// 获取病友圈详情
export const getSickCircleDetail = (sickCircleId: number, userId?: number, sessionId?: string) => {
    console.log('API request - getSickCircleDetail:', { sickCircleId, userId, sessionId });

    const headers: Record<string, string | number> = {};
    if (userId) headers.userId = userId;
    if (sessionId) headers.sessionId = sessionId;

    return request({
        url: '/health/user/sickCircle/v1/findSickCircleInfo',
        method: 'get',
        headers,
        params: { sickCircleId }
    }).then(response => {
        console.log('API response - getSickCircleDetail:', response);
        return response;
    }).catch(error => {
        console.error('API error - getSickCircleDetail:', error);
        throw error;
    });
};

// 发布病友圈评论
export const publishSickCircleComment = (data: {
    sickCircleId: number;
    content: string;
    parentId?: number;
    replyUserId?: number;
}) => {
    return request({
        url: '/health/user/sickCircle/v1/publishSickCircleComment',
        method: 'post',
        data
    });
};
